package com.dengyafei.utils;


import com.dengyafei.constant.RecommendConstant;
import com.dengyafei.constant.RedisKeyConstant;
import com.dengyafei.helper.BeanHelper;
import com.dengyafei.result.RedisLoginEntity;
import io.lettuce.core.models.role.RedisSentinelInstance;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author 邓亚非
 */
public class RedisListUtil {


    /**
     * 重置/修改密码
     *
     * @param redisTemplate
     * @param key
     * @param redisLogin
     * @return
     */
    public static boolean updateRedisList(RedisTemplate<Object, Object> redisTemplate, String key, RedisLoginEntity redisLogin) {
        Long size = redisTemplate.opsForList().size(key);
        List<Object> range = redisTemplate.opsForList().range(key, 0, size);
        List<RedisLoginEntity> redisLoginEntities = BeanHelper.copyWithCollection(range, RedisLoginEntity.class);
//        如果redis 查询为null或者key 过期导致数据丢失返回9999
        if (CollectionUtils.isEmpty(range)) {
            return true;
        }
        long index = 0;
        for (int i = 0; i < redisLoginEntities.size(); i++) {
            if (redisLoginEntities.get(i).getUsername().equals(redisLogin.getUsername())) {
                index = i;
            }
        }
        try {
            redisTemplate.opsForList().set(key, index, redisLogin);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 登录接口读取缓存
     *
     * @param redisTemplate
     * @param key
     * @param redisLoginEntity
     * @return
     */
    public static Integer getLogin(RedisTemplate<Object, Object> redisTemplate, String key, RedisLoginEntity redisLoginEntity) {
        Long size = redisTemplate.opsForList().size(key);
        List<Object> range = redisTemplate.opsForList().range(key, 0, size);
        List<RedisLoginEntity> redisLoginEntities = BeanHelper.copyWithCollection(range, RedisLoginEntity.class);
//        如果redis 中没有或者 key 过期导致数据删除;返回10000
        if (CollectionUtils.isEmpty(range)) {
            return RecommendConstant.REDIS_NULL;
        }
        RedisLoginEntity redisLogin = null;
        for (int i = 0; i < redisLoginEntities.size(); i++) {
            if (redisLoginEntity.getUsername().equals(redisLoginEntities.get(i).getUsername())) {
                redisLogin = redisLoginEntities.get(i);
                //        判断密码是否正确
                if (!redisLogin.getPassword().equals(redisLoginEntity.getPassword())) {
                    return RecommendConstant.PASSWORD_ERROR;
                }
                return RecommendConstant.LOGIN_SUCCESS;
            }
        }
        return RecommendConstant.NOT_REGISTER;
    }

    /**
     * 添加一个分享 key
     *
     * @param redisTemplate
     * @param key
     */
    public static void addShareKey(RedisTemplate<Object, Object> redisTemplate, String key) {
        List<Object> range = redisTemplate.opsForList().range(RedisKeyConstant.ShareRedis.SHARE_KEY, 0, -1);
        for (int i = 0; i < range.size(); i++) {
            if (range.get(i).equals(key)) {
                return;
            }
        }
        try {
            Long aLong = redisTemplate.opsForList().leftPush(RedisKeyConstant.ShareRedis.SHARE_KEY, key);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
